package com.jiaai.shop;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.jiaai.shop.store.entity.*;
import com.jiaai.shop.store.service.*;
import com.jiaai.shop.util.SpringUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.math.BigDecimal;
import java.util.Calendar;
import java.util.Date;

import static com.jiaai.shop.util.CacheManage.getSystemConfig;

/**
 * Created by Administrator on 2018\5\5 0005.
 */
public class OnPayThread implements Runnable {
    private String orderNo;

    public OnPayThread(String orderNo) {
        this.orderNo = orderNo;
    }
    @Override
    public void run() {
        try {
            SOrder order=  getOrderService().selectOne(new EntityWrapper().eq("orderId",orderNo));
            SGoods goods=getGoodsService().selectById(order.getGoodsId());
            order.setStatus(1);
            getOrderService().updateById(order);
            TMemberGoods tMemberGoods=new TMemberGoods();
            tMemberGoods.setBuyTime(new Date());
            tMemberGoods.setGoodsId(order.getGoodsId());
            tMemberGoods.setIntegral(goods.getIntegral());
            Calendar calendar=Calendar.getInstance();
            calendar.setTime(new Date());
            calendar.set(Calendar.DAY_OF_MONTH,calendar.get(Calendar.DATE)+1);//让日期加1
            tMemberGoods.setIntegralEffectivetime(calendar.getTime());
            tMemberGoods.setNum(1);
            tMemberGoods.setGoodsPrice(goods.getGoodsPrice());
            tMemberGoods.setPresentedIntegral(goods.getPresentedIntegral());
            tMemberGoods.setmId(order.getMemberId());
            getMemberGoodsService().insert(tMemberGoods);

            SMember member=getMemberService().selectById(order.getMemberId());
            member.setTotalScore(member.getTotalScore()+goods.getPresentedIntegral());
            member.setPresentScore(member.getPresentScore()+goods.getPresentedIntegral());
            member.setConsume(1);
            getMemberService().updateById(member);
            TIntegralHistory integralHistory=new TIntegralHistory();
            integralHistory.setDescribe("购买"+goods.getGoodsName()+",获得系统奖励"+goods.getPresentedIntegral()+"积分");
            integralHistory.setMemberId(member.getMemberId());
            integralHistory.setType(1);
            integralHistory.setNum(goods.getPresentedIntegral());
            integralHistory.setTime(new Date());
            getIntegralHistoryService().insert(integralHistory);
            if(StringUtils.isEmpty(member.getInvitationPer())){
                SMember  perMember=getMemberService().selectById(member.getInvitationPer());
                Integer score=goods.getGoodsPrice().multiply( BigDecimal.valueOf(getSystemConfig().getRecommendInter().doubleValue())).intValue();
                perMember.setPresentScore(perMember.getPresentScore()+score);
                perMember.setTotalScore(perMember.getTotalScore()+score);
                getMemberService().updateById(perMember);
                TIntegralHistory pintegralHistory=new TIntegralHistory();
                pintegralHistory.setDescribe("用户购买"+goods.getGoodsName()+",获得佣金奖励"+goods.getPresentedIntegral()+"积分");
                pintegralHistory.setMemberId(perMember.getMemberId());
                pintegralHistory.setType(3);
                pintegralHistory.setNum(score);
                pintegralHistory.setTime(new Date());
                getIntegralHistoryService().insert(pintegralHistory);
            }
        }catch (Exception e){
            System.out.println(e.getMessage());
        }



    }

    public ISGoodsService getGoodsService() {
        return SpringUtil.getBean(ISGoodsService.class);
    }

    public ISOrderService getOrderService() {
        return SpringUtil.getBean(ISOrderService.class);
    }
    public ITMemberGoodsService getMemberGoodsService() {
        return SpringUtil.getBean(ITMemberGoodsService.class);
    }
    public ISMemberService getMemberService() {
        return SpringUtil.getBean(ISMemberService.class);
    }

    public ITIntegralHistoryService getIntegralHistoryService() {
        return SpringUtil.getBean(ITIntegralHistoryService.class);
    }
}
